<?php
/**
 * Aero - Xistins Technologies PHP Framework
 * 
 * LICENSE
 * This source file is subject to the New BSD license that is bundled 
 * with this package in the LICENSE.txt. It is also available through 
 * the world-wide-web at this URL: http://dev.xistins.com/license/production
 * If you did not receive a copy of this license and are unable to obtain
 * it through the world-wide-web, please send an email to 
 * license@xistins.com so we can send you a copy immediately.
 * 
 * @category   Aero
 * @package    Aero_Registry
 * @copyright  http://dev.xistins.com/license/production     New BSD
 * @version    $Id: Registry.php 4 2009-11-14 18:35:45Z csouth@xistins.com $
 */

/**
 * Variable storage for passing between scripts
 * 
 * ------------------------------------------------------------------------------------- 
 * I wrote this class mostly for its value during the development process if you need to 
 * track down a variable. It may not make it to the final release but if anyone would 
 * like to see it stay please let me know.
 * -------------------------------------------------------------------------------------
 * 
 * @category   Aero
 * @package    Aero_Registry
 * @copyright  http://dev.xistins.com/license/production     New BSD
 * @version    $Id: Registry.php 4 2009-11-14 18:35:45Z csouth@xistins.com $
 */
class Aero_Registry
{
    /**
     * History of variables
     * @var array
     */
    protected static $_history = array();

    /**
     * Variables
     * @var array
     */
    protected static $_data    = array();

    /**
     * Set a variable
     * 
     * @param  string $name
     * @param  mixed  $value
     * @return mixed
     */
    public static function set($name, $value)
    {
        $backtrace = debug_backtrace();
        $type      = (!isset(self::$_data[$name])) ? 'set' : 'modify';
        $history   = array('var'           => $name,
                           'value'         => $value,
                           'setInFilename' => $backtrace[0]['file'],
                           'setOnLine'     => $backtrace[0]['line'],
                           'status'        => $type);
        self::$_history[] = $history;

        self::$_data[$name] = $value;
    }

    /**
     * Unset a variable
     * 
     * @param  string #name
     * @return void
     */
    public static function remove($name)
    {
        $backtrace = debug_backtrace();
        $history = array('var'           => $name,
                         'setInFilename' => $backtrace[0]['file'],
                         'setOnLine'     => $backtrace[0]['line'],
                         'status'        => 'unset');
        self::$_history[] = $history;
        unset(self::$_data[$name]);
    }

    /**
     * Get a variable
     */
    public static function get($name)
    {
        $backtrace = debug_backtrace();
        $history   = array('var'           => $name,
                           'value'         => $value,
                           'setInFilename' => $backtrace[0]['file'],
                           'setOnLine'     => $backtrace[0]['line'],
                           'status'        => 'get');
        self::$_history[] = $history;

        return self::$_data[$name];
    }

    /**
     * Get a variables history
     * 
     * @param  string $name
     * @return array
     */
    public static function getVarHistory($name)
    {
        $varHistory = array();
        foreach(self::$_history as $history) {
            if($history['var'] = $name) {
                $varHistory[] = $history;
            }
        }
        return $varHistory;
    }
}